<?php

class McCube_Notifier_Browser_Html
    extends McCube_Notifier_Browser {

    protected $_counter = 1;
    protected static $_cssSent = false;

    /** */
    public function render($title, $text, $backtraces=false){
        return $this->_js().$this->_css()
            .'<div class="McCube-Notifier">'
                .'<h3>'.$this->_decorator($title).'</h3>'
                .'<div>'.$this->_decorator(nl2br($text)).'</div>'
                .($backtraces ? $this->_buildBacktraces($backtraces) : '')
            .'</div>'
        ;
    }

    /** */
    protected function _css(){
        //if(!(self::$_cssSent)){
        //    self::$_cssSent = true;
            return '<style type="text/css">'
                .'DIV.McCube-Notifier *'                  .'{font-size:100%;font-family:Arial;}'
                .'DIV.McCube-Notifier H3'                 .'{font-size:100%;}'
                .'DIV.McCube-Notifier'                    .'{font-size:100%;margin:10px;padding:10px;background-color:beige;border:1px solid red;}'
                .'DIV.McCube-Notifier .backtrace'         .'{font-size:90%;border-top:1px solid silver;padding:5px 0 0 5px;margin-top:5px;}'
                .'DIV.McCube-Notifier TABLE'              .'{border-collapse:collapse;}'
                .'DIV.McCube-Notifier TABLE'              .'{background:white;margin-top:5px;}'
                .'DIV.McCube-Notifier TABLE TD.line'      .'{width:25px;color:green;font-weight:bold;text-align:right;padding-right:10px !important;}'
                .'DIV.McCube-Notifier TABLE TH.vertical'  .'{width:40px;text-align:center;}'
                .'DIV.McCube-Notifier TABLE TD SPAN'      .'{font-weight:bold;color:brown;}'
                .'DIV.McCube-Notifier TABLE TD.class'     .'{width:75px;}'
                .'DIV.McCube-Notifier TABLE TD.member'    .'{width:10px;text-align:center;}'
                .'DIV.McCube-Notifier TABLE TH.list'      .'{color:#808080;}'
                .'DIV.McCube-Notifier TABLE TD
                    ,DIV.McCube-Notifier TABLE TH'        .'{min-width:25px;font-size:85%;white-space:nowrap;padding:4px !important;border:1px solid gray;text-align:left;}'
            .'</style>'."\n";
        //}
    }

    /** */
    protected function _js(){
        //if(!(self::$_cssSent)){
            return '<script type="text/javascript">'
                .'McCubeNotifierDisplayBT = function(name){'
                    .'var obj = document.getElementById(name);'
                    .'obj.style.display = obj.style.display == \'none\' ? \'block\' : \'none\';'
                .'}'
            .'</script>'."\n";
        //}
    }

    /** */
    protected function _buildBacktraces($backtraces){
        $backtraces = $this->_getBacktraces($backtraces);
        if(!$backtraces) return;

        $lastBacktrace  = array_shift($backtraces);
        if(isset($backtraces[0])){
            unset($lastBacktrace['class']);
            $lastBacktrace['function'] = $backtraces[0]['function'];
            if(isset($backtraces[0]['class'])){
                $lastBacktrace['type' ] = $backtraces[0]['type'];
                $lastBacktrace['class'] = $backtraces[0]['class'];
            }

            if(!isset($lastBacktrace['file'])){
                $lastBacktrace['file'] = $backtraces[0]['file'];
                $lastBacktrace['line'] = $backtraces[0]['line'];
                unset($backtraces[0]);
            }
        }

        $echo = '';
        foreach($backtraces as $backtrace){

            $echo .= '<tr>';
            switch(isset($backtrace['class'])){
                default      : $echo .= '<td colspan="3">'.$backtrace['function'].'</td>';break;
                case    true :
                    $echo .= ''
                          .'<td class="class">'.$backtrace['class'].'</td>'
                          .'<td class="member"">' .$backtrace['type'].'</td>'
                          .'<td>'.$backtrace['function'].'</td>'
                          ;
                break;
            }

            switch(isset($backtrace['file'])){
                default      : $echo .= '<td colspan="2">&nbsp;</td>';break;
                case    true :
                    $echo .= ''
                          .'<td>'.$backtrace['file'].'</td>'
                          .'<td class="line">'.$backtrace['line'].'</td>'
                          ;
                break;
            }
            $echo .= '</tr>';
        }

        $function = '<span style="color:green;">';
        if(isset($lastBacktrace['class']))
            $function .= $lastBacktrace['class'].$lastBacktrace['type'];

        $function .= $lastBacktrace['function'].'</span>';

        if(isset($lastBacktrace['file']))
            $function .= '<br /> - <span style="color:brown;font-style:italic;">'.$lastBacktrace['file'].' : '.$lastBacktrace['line'].'</span>';

        $return =
        '<div class="backtrace" style="">'
            .'Trace : <a href="javascript:void(0);" onclick="McCubeNotifierDisplayBT(\'backtraces'.$this->_counter.'\')">'.$function.'</a>'
        .'</div>'
        .'<table id="backtraces'.$this->_counter++.'" cellspacing="0" cellpadding="0" border="0" style="width:100%;display:none;">'
            .'<thead>'
                .'<tr>'
                    .'<th class="list" colspan="3">Function</th>'
                    .'<th class="list">File</th>'
                    .'<th class="list">Line</th>'
                .'</tr>'
            .'</thead>'
            .'<tbody>'.$echo.'</tbody>'
        .'</table>'
        ;

        return $return;
    }

    protected function _decoratorCallback($match){
        switch($match[1]){
            case 'value' : $style = 'color:green;' ;break;
            case 'bundle': $style = 'color:blue;'  ;break;
            case 'error' : $style = 'color:red;'   ;break;
            case 'alert' : $style = 'color:orange;';break;
            case 'notice': $style = 'color:blue; ' ;break;
        }
        return '<span style="'.$style.'">' .$match[2].'</span>';
    }

}